// 二叉搜索树
class BinarySearchTree{
    public TreeNode root = null;
    class TreeNode{
        public int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val){
            this.val = val;
        }

        @Override
        public String toString() {
            return "delNode.val:" + this.val;
        }
    }
    // 查找
    public TreeNode search(int val){
        if (root == null){
            return null;
        }

        TreeNode cur = root;
        while (cur != null){
            if (val < cur.val) {
                cur = cur.left;
            } else if (val > cur.val) {
                cur = cur.right;
            }else {
                return cur;
            }
        }
        return null;
    }

    // 增加
    public boolean insert(int val){
        if (root == null){
            root = new TreeNode(val);
            return true;
        }

        // 走到这里说明root!=null
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null){
            if(val < cur.val){
                parent = cur;
                cur = cur.left;
            }else if (val > cur.val){
                parent = cur;
                cur = cur.right;
            }else {  // 注意：val值已经有了就不需要再插入了
                return false;
            }
        }
        // 注意：出循环后cur=null,parent是val的父节点
        if(val < parent.val){
            parent.left = new TreeNode(val);
        }else {
            parent.right = new TreeNode(val);
        }
        return true;
    }

    // 删除
    public void remove(int val){
        TreeNode cur = root;
        TreeNode parent = null;

        while (cur != null){
            if (val < cur.val) {
                parent = cur;
                cur = cur.left;
            }else if (val > cur.val){
                parent = cur;
                cur = cur.right;
            }else {
                removeCur(parent,cur);
                return;  //注意删除完后，直接return
            }
        }
    }
    private void removeCur(TreeNode parent,TreeNode cur){
        if (cur.left == null){
            if (cur == root){
                root = cur.right;
            } else if (cur == parent.left){
                parent.left = cur.right;
            }else if (cur == parent.right){
                parent.right = cur.right;
            }
        }else if(cur.right == null){
            if (cur == root) {
                root = cur.left;
            }else if (cur == parent.left){
                parent.left = cur.left;
            } else if (cur == parent.right) {
                parent.right = cur.left;
            }
        }else {
            TreeNode target = cur.left;
            TreeNode targetParent = cur;
            while (target.right != null){
                targetParent = target;
                target = target.right;
            }

            // 注意：走到这里，target就是以cur的左子树为根节点的最右边的节点，即cur左子树的最大值。
            // 当前操作：把target的val拷贝到cur节点，然后删除target
            cur.val = target.val;
            if (target == targetParent.left){
                targetParent.left = target.left;
            } else if (target == targetParent.right) {
                targetParent.right = target.left;
            }
        }
    }
}


public class Main {
    public static void inOrderTree(BinarySearchTree.TreeNode root){
        if(root == null){
            return;
        }
        inOrderTree(root.left);
        System.out.print(root.val + " ");
        inOrderTree(root.right);
    }

    public static void main(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();

        int[] array = {1,3,6,8,2,5,7,9};
        for (int i = 0; i < array.length; i++) {
            binarySearchTree.insert(array[i]);
        }

        binarySearchTree.remove(1);

        inOrderTree(binarySearchTree.root); // 中序遍历查看数据
    }
}
